<?php

namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\UserManual;
use think\Db;
/**
 * 答疑 包含 AI 答疑 人工答疑
 */
class Faq extends Api
{
  

    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /** ------------------人工答疑--------------------------- */

    public function checkPopup()
    {
        $user_id = $this->auth->id;
        $user_manual = (new UserManual())->getUserValidManual($user_id);
        if ($user_manual && count($user_manual) > 0) {
            $need_popup  = false;
        } else {
            $need_popup  = true;
        }
        $this->success("OK", [
            'need_popup' => $need_popup,
            'data' => ['manual_pic' => config('site.manual_pic'), 'manual_con' => config('site.manual_con')]
        ]);
    }

    /**
     * 人工答疑
     * @return void
     */
    public function manual() { 
        $user_id = $this->auth->id;
        $content = $this->request->post('content');
        $pic = $this->request->post('pic');
        if(!$content){
            $this->error("请输入内容");
        }
        # 查询用户资源
        $info = (new UserManual())->where('user_id', $user_id)->find();
        if(!$info){ 
            $this->error('您没有资源信息');
        }
        if($info->end_time < time()){
            $this->error('人工答疑已截止码有效期');
        }
        $data = [
            'user_id' => $user_id,
            'teacher_id' => $info->teacher_id,
            'content' => $content,
            'pic' => $pic,
            'createtime' => time(),
            'status' => 1
        ];
        $row = Db::name('manual')->insert($data);
        if($row){
            $this->success('提交成功');
        }else{
            $this->error('提交失败');
        }
    }


    /**
     * 答疑列表
     * @return void
     */
    public function getManualList(){ 
        $user_id = $this->auth->id;
        $list = Db::name('manual')->where('user_id',$user_id)->order('createtime desc')->select();
        foreach ($list as &$item) {
            if($item['pic']){
                $item['pic'] = explode(',',$item['pic']);
            }else{
                $item['pic'] = [];
            }
            if($item['photo']){ 
                $item['photo'] = explode(',',$item['photo']);
            }else{
                $item['photo'] = [];
            }
        }
        $this->success('ok',$list);
    }

    













}
